// 引入数据库
const conn = require("../../data/index");

module.exports.client = (req, res) => {
  if (!req.session.isLogin) {
    res.redirect("/admin/login");
    return;
  }
  return res.render("background/client");
};
// 分页
module.exports.showData = (req, res) => {
  let pageStart = (req.query.pageNum - 1) * req.query.pageSize;
  conn.query(
    `
  select count(*) num from leftAside where name like '%${req.query.keyword}%';
  select * from leftAside where name like '%${
    req.query.keyword
  }%' order by status limit ${pageStart},${req.query.pageSize}`,
    (error, data) => {
      if (error) return console.log(error);
      return res.json(data);
    }
  );
};

// 删除
module.exports.delete = (req, res) => {
  conn.query(
    "delete from leftAside where id = ?",
    [req.query.id],
    (error, data) => {
      if (error) return console.log(error);
      return res.json({
        code: "1001",
        msg: "数据删除成功"
      });
    }
  );
};

//  回显
module.exports.find = (req, res) => {
  conn.query(
    "select * from leftAside where id = ?",
    [req.query.id],
    (error, data) => {
      if (error) return console.log(error);
      return res.json(data);
    }
  );
};

// 修改
module.exports.update = (req, res) => {
  let parmas = [
    req.body.name,
    req.body.telephone,
    req.body.sex,
    req.body.site,
    req.body.status,
    req.body.details,
    req.body.id
  ];
  conn.query(
    "update leftAside set name = ? , telephone = ?, sex = ?,site = ?,status = ?,details = ? where id = ?",
    parmas,
    (error, data) => {
      if (error) return console.log(error);
      return res.json({
        code: "1002",
        msg: "数据修改成功"
      });
    }
  );
};
